#!/bin/bash

OPENGROK_DIR=/mnt/data/opengrok
REPO_DIR=${OPENGROK_DIR}/src
PD=$(dirname $0)
HIS="yes"
SYNC_PROJ_LIST=/tmp/sync_proj.list
FLAG=$1
FLAG2=$2
code_dir=(linux-stable openEuler-22.03-LTS-SP1)

if [ "X${FLAG}" != "Xnosync" ]; then
for code in ${code_dir[*]}
do
	pushd ${REPO_DIR}/$code &>/dev/null
	echo "syncing $code ..."
	git pull origin
	popd &>/dev/null
done
echo "sync code done"
fi

if [ "X${FLAG2}" != "Xnoindex" ]; then

echo "update grok index for each project ..."
ls  ${REPO_DIR} > ${SYNC_PROJ_LIST}

    while read i
    do
    if [ "X${FLAG2}" == "X" -o "X${FLAG2}" == "Xgitproj" ]; then
        if [[ ${code_dir[@]/$i/} != ${code_dir[@]} ]]; then
        echo "Updating index $i"
        ${PD}/update_index_per_project.sh "$i"
        fi
    fi

    if [ "X${FLAG2}" == "X" -o "X${FLAG2}" == "Xproj" ]; then
        if [[ ${code_dir[@]/$i/} == ${code_dir[@]} ]]; then
        echo "Updating index $i"
        ${PD}/update_index_per_project.sh "$i"
        fi
    fi
    done < "${SYNC_PROJ_LIST}"

    fi

echo "All done!"
